package com.example.waimai.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.waimai.common.CustomEX;
import com.example.waimai.dto.SetmealDto;
import com.example.waimai.mapper.SetmealMapper;
import com.example.waimai.pojo.Setmeal;
import com.example.waimai.pojo.SetmealDish;
import com.example.waimai.service.ServiceSetmeal;
import com.example.waimai.service.ServiceSetmealDish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements ServiceSetmeal{

    @Autowired
    private ServiceSetmealDish serviceSetmealDish;

    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，判断是否可以删除
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        Long count = this.count(lqw);
        //如果不能，则抛出
        if(count>0){
            throw new CustomEX("套餐正在售卖，不能删除");
        }

        //删除数据
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> lqw1=new LambdaQueryWrapper<>();
        lqw1.in(SetmealDish::getDishId,ids);
        serviceSetmealDish.remove(lqw1);


    }

    //新增套餐，同时保存套餐和菜品的管理关系
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        serviceSetmealDish.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getByidWithDish(Long id) {
        Setmeal setmealid = this.getById(id);
        SetmealDto setmealDto=new SetmealDto();

        //拷贝
        BeanUtils.copyProperties(setmealid,setmealDto);

        LambdaQueryWrapper<SetmealDish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getDishId,id);
        List<SetmealDish> list=serviceSetmealDish.list(lqw);

        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    //提交修改结果
    public void updataByidWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);//更新基本信息
        // 先删除原来的套餐所对应的菜品数据。
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        serviceSetmealDish.remove(queryWrapper);

        List<SetmealDish> flavor=setmealDto.getSetmealDishes();
        flavor.stream().map((item)->{
            item.setDishId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());//保存id
        serviceSetmealDish.updateBatchById(flavor);
    }


}
